package likou;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2022/01/06 19:58
 */
public class _71 {
    public static void main(String[] args) {
        _71 one = new _71();
        System.out.println(one.simplifyPath("/a/./b/../../c/"));
    }
    public String simplifyPath(String path) {
        int d = 0;
        int len = path.length();
        int i = len - 1;
        if (i == -1) return "/";
        StringBuilder res = new StringBuilder();
        for (; i > 0; ) {
            while (i >= 0 && path.charAt(i) == '/') {
                i--;
            }
            int[] is = next(path, i);
            switch (is[1]) {
                case 0://单词
                    if (d > 0) {
                        d--;
                    } else {
                        res.insert(0, path.substring(is[0], i + 1));
                    }
                    break;
                case 1://无效，不用管
                    break;
                case 2://上一层
                    d++;
                    break;
                default:
                    break;

            }
            i = is[0];
        }
        if (res.length()==0) return "/";
        return res.toString();
    }

    private int[] next(String path, int i) {
        if (i<1) return new int[]{i,1};
        if (path.charAt(i) == '.') {
            int dianNum = 0;
            while (i>0&&path.charAt(i)=='.'){
                dianNum++;
                i--;
            }
            return dianNum<3?new int[]{i,dianNum}:new int[]{i,0};
        } else {
            while (i > 0 && path.charAt(i) != '/') {
                i--;
            }
            return new int[]{i, 0};
        }

    }
}
